
#include "nane/SerializeSystem/SerializeSystem.h"
#include "nane/SerializeSystem/SerializeSystemImpl_.h"
#include "nane/SerializeSystem/SerializationProtocolImpl_.h"
#include "nane/core/assert.h"
#include "nane/core/log.h"

namespace nane
{
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SerializeSystem::SerializeSystem()
        : SubSystem<ISerializeSystem>( new Implementation() )
        , m( static_cast<Implementation*>(SubSystem<ISerializeSystem>::m) )
    {
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SerializeSystem::~SerializeSystem()
    {
        Shutdown();
        delete m;
        m = NULL;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    bool SerializeSystem::Deserialize( const InputPtr& _input, ISerializationProtocol* _protocol, byte* _buffer, uint32 _bufferSize )
    {
        NANE_ASSERT_RV( m->interface != NULL, "SerializeSystem must be initialized", false );
        NANE_ASSERT_RV( _input != NullPtr, "Input must be valid", false );
        NANE_ASSERT_RV( _protocol != NULL, "SerializationProtocol must be valid", false );
        NANE_ASSERT_RV( _buffer != NULL, "Buffer must be valid", false );
        return m->interface->Deserialize(_input.Get(), _protocol, _buffer, _bufferSize);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SerializationProtocol SerializeSystem::CreateProtocol( const OutputPtr& _output )
    {
        NANE_ASSERT_RV( m->interface != NULL, "SerializeSystem must be initialized", NullPtr );
        NANE_ASSERT_RV( _output != NullPtr, "Output must be valid", NullPtr );
        SerializationProtocol::Implementation* serializationProtocolImpl = NULL;
        ISerializationProtocol* serializationProtocolInterface = m->interface->CreateProtocol(_output.Get());
        if( serializationProtocolInterface != NULL )
        {
            serializationProtocolImpl = new SerializationProtocol::Implementation(m, serializationProtocolInterface);
        }
        return SerializationProtocol(serializationProtocolImpl);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
}   // namespace nane
